package com.lagou.edu.service.impl;

import com.lagou.edu.dao.AccountDao;
import com.lagou.edu.pojo.Account;
import com.lagou.edu.service.TransferService;
import com.lagou.edu.stereotype.Autowired;
import com.lagou.edu.stereotype.Service;
import com.lagou.edu.stereotype.Transactional;
import com.lagou.edu.utils.TransactionManager;

/**
 * @author 应癫
 */
@Service(value = "transferService")
public class TransferService2Impl implements TransferService {

    // 最佳状态
    @Autowired
    private AccountDao aDto;
    @Autowired
    private TransactionManager transactionManager;

    @Transactional
    @Override
    public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {

        /*try {
            // 开启事务(关闭事务的自动提交)
            transactionManager.beginTransaction();*/

        Account from = aDto.queryAccountByCardNo(fromCardNo);
        Account to = aDto.queryAccountByCardNo(toCardNo);

        from.setMoney(from.getMoney() - money);
        to.setMoney(to.getMoney() + money);

        aDto.updateAccountByCardNo(to);
        int c = 1 / 0;
        aDto.updateAccountByCardNo(from);

        // 提交事务

        //transactionManager.commit();
        /*} catch (Exception e) {
            e.printStackTrace();
            // 回滚事务
            transactionManager.rollback();

            // 抛出异常便于上层servlet捕获
            throw e;

        }*/


    }
}
